//
//  HamatonLoginViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/3/20.
//

import UIKit
import SnapKit
import Toast_Swift

class HamatonLoginViewController: UIViewController {

    private let loginViewModel: HamatonLoginViewModel = HamatonLoginViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = Theme.Color.Background.default
        view.addSubview(topView)
        view.addSubview(loginInputView)
        view.addSubview(bottomView)
        view.addSubview(readAndAgreeView)
        view.addSubview(xieyiyuyinsiView)
        
        topView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(200)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(40)
        }
        
        loginInputView.snp.makeConstraints { make in
            make.top.equalTo(topView.snp.bottom).offset(10)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(160)
        }
        
        readAndAgreeView.snp.makeConstraints { make in
            make.top.equalTo(loginInputView.snp.bottom).offset(20)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(160)
        }
        
        bottomView.snp.makeConstraints { make in
            make.bottom.equalTo(view.snp.bottom).offset(-20)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(40)
        }
        xieyiyuyinsiView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(0)
            make.bottom.equalToSuperview().offset(0)
            make.left.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(0)
            make.height.equalTo(100)
        }
        
        xieyiyuyinsiView.isHidden = true
    }
    // logo
    lazy var topView: HamatonTopView = {
        let view = HamatonTopView()
        return view
    }()
   // 用户名密码
    lazy var loginInputView: HamatonLoginInputView = {
        let view = HamatonLoginInputView()
        view.isUserInteractionEnabled = true
        view.delegate = self
        return view
    }()
    // 登录与用户协议
    lazy var readAndAgreeView: HamatonReadAndAgreeView = {
        let view = HamatonReadAndAgreeView()
        view.isUserInteractionEnabled = true
        view.delegate = self
        return view
    }()
    // 中英文切换
    lazy var bottomView: HamatonBottomView = {
        let view = HamatonBottomView()
        view.isUserInteractionEnabled = true
        view.delegate = self
        return view
    }()
    
    // 协议与隐私政策
    lazy var xieyiyuyinsiView: HamatonXieyiyuyinsiView = {
        let view = HamatonXieyiyuyinsiView()
        view.isUserInteractionEnabled = true
        view.delegate = self
        return view
    }()

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK: --HamatonBottomViewDelegate
extension HamatonLoginViewController: HamatonBottomViewDelegate {
    
    func hamatonBottomViewChangeLanguage() {
       
        print("hamatonTopViewChangeLanguage111")
        
        let alertController = UIAlertController(title: NSLocalizedString("语言设置", tableName: "HamatonLocalizable", comment: ""), message: nil, preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: NSLocalizedString("取消", tableName: "HamatonLocalizable", comment: ""), style: .cancel, handler: nil)
        let deleteAction = UIAlertAction(title: NSLocalizedString("中文", tableName: "HamatonLocalizable", comment: ""), style: .default) { action in
            if HamatonLanguageManager.shared.language != .Chinese {
                HamatonLanguageManager.shared.language = .Chinese
                HamatonLanguageManager.saveLanguage(chooseLanguage: .Chinese)
            }
        }
         
        let archiveAction = UIAlertAction(title: NSLocalizedString("英文", tableName: "HamatonLocalizable", comment: ""), style: .default) { action in
            if HamatonLanguageManager.shared.language != .English {
                HamatonLanguageManager.shared.language = .English
                HamatonLanguageManager.saveLanguage(chooseLanguage: .English)
            }
        }
         
        alertController.addAction(cancelAction)
         
        alertController.addAction(deleteAction)
         
        alertController.addAction(archiveAction)
         
        self.present(alertController, animated: true, completion: nil)

    }
    
}

// MARK: --HamatonReadAndAgreeViewDelegate
extension HamatonLoginViewController: HamatonReadAndAgreeViewDelegate {
    
    func hamatonReadAndAgreeView(view: HamatonReadAndAgreeView, isSelectd: Bool) {
        print("hamatonReadAndAgreeView")
        if view == self.readAndAgreeView {
            loginViewModel.isAgree = isSelectd
        }
    }
    
    func hamatonLoginBottomViewClickLogin(view: HamatonReadAndAgreeView) {
        if view == self.readAndAgreeView {
            if let message = self.loginViewModel.jundgeIsLogin() {
                self.view.makeToast(message, duration: 3.0, position: .center)
            } else {
                // 登录逻辑处理
                print("登录逻辑处理")
                HamatonLoginManager.shared.updateToken(token: "TokenNew")
                if UIApplication.shared.delegate != nil {
                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.resetRootViewController()
                }
            }
        }
    }
    
    func xieyiyuyinsi() {
        xieyiyuyinsiView.isHidden = false
    }
    
}

// MARK: --HamatonLoginInputViewDelegate
extension HamatonLoginViewController: HamatonLoginInputViewDelegate {
    func hamatonLoginInputView(view: HamatonLoginInputView, nameText: String?, type: HamatonLoginInptutType) {
        print(nameText ?? "")
        if type == .name {
            self.loginViewModel.userName = nameText ?? ""
        } else {
            self.loginViewModel.userPassword = nameText ?? ""
        }
    }
}

// MARK: --HamatonXieyiyuyinsiViewDelegate
extension HamatonLoginViewController: HamatonXieyiyuyinsiViewDelegate {
    func tongyiHamatonXieyiyuyinsi() {
        xieyiyuyinsiView.isHidden = true
    }
}
